package com.wheel.autoconfigure.web.mvc.returnvaluehandler;/*
 * <h3>Title:ai-chiefcomplaint系统</h3>
 * <p>Copyright (c) 2021, wnfbx. All rights reserved.</p>
 * @author wnfbx
 */

import com.wheel.autoconfigure.web.mvc.filter.ResponseTimeFilter;
import com.wheel.core.result.Result;
import com.wheel.core.time.WheelDateTime;
import com.wheel.core.utils.Log;
import org.slf4j.MDC;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;

/**
 * <dt>Description:ResultMethodProcessor</dt>
 * <dd>通用result处理</dd>
 *
 * @author wnfbx
 * @version 1.0 <i>2021/8/5 19:30</i>
 * @since jdk1.7+
 */
public abstract class ResultMethodProcessor {

    protected static void formatResult(Object returnValue, NativeWebRequest webRequest){
        if (returnValue != null) {
            Result<?> r = (Result<?>) returnValue;
            r.setRequestId(MDC.get(Log.RequestId));
            long nowTimestamp = WheelDateTime.nowTimestamp();
            r.setTimestamp(nowTimestamp);
            long requestTimestamp = (long) webRequest.getAttribute(ResponseTimeFilter.REQUEST_TIMESTAMP_KEY, RequestAttributes.SCOPE_REQUEST);
            r.setResponseTime(nowTimestamp - requestTimestamp);
        }
    }
}
